import psutil
from threading import Thread


def task():
    while True:
        per = psutil.cpu_percent(interval=1)
        print(per)


if __name__ == '__main__':
    for i in range(100):
        t = Thread(target=task)
        t.start()
# while True:
#     cpu_per = psutil.cpu_percent(interval=1, percpu=True)
#     print('cpu:', max(cpu_per))
#     memory_per = psutil.virtual_memory().percent
#     print('memory:', memory_per)


# def cpu_per(request):
#     if request.method == 'GET':
#         percent = max(psutil.cpu_percent(interval=1, percpu=True))  # 多核中的cpu利用率最大值
#         # percent = psutil.cpu_percent(interval=1)  # cpu平均利用率最大值
#         use_memory = psutil.virtual_memory()[2]
#         used = str(psutil.virtual_memory()[3] / 1024000).split('.')[0]
#         free = str(psutil.virtual_memory()[4] / 1024000).split('.')[0]
#         current_time = time.strftime('%H:%M:%S')
#         res = {'per': percent, 'time': current_time, 'memory': [use_memory, 100 - use_memory],
#                'used_free': [used, free]}
#         return JsonResponse(res, safe=False)
